//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.siecom.tools;

import android.util.Log;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataParseUtil {
    public DataParseUtil() {
    }

    public static String formatFromCentsToYuan(String numStrInUtilsOfCents) {
        if(numStrInUtilsOfCents == null) {
            return null;
        } else if(numStrInUtilsOfCents.equals("")) {
            return "";
        } else {
            Pattern pattern = Pattern.compile("[0-9]*");
            Matcher isNum = pattern.matcher(numStrInUtilsOfCents);
            if(!isNum.matches()) {
                throw new RuntimeException("纯数字字符串：\"" + numStrInUtilsOfCents + "\"，中出现非数字字符");
            } else {
                StringBuilder builder = new StringBuilder(numStrInUtilsOfCents);
                if(builder.length() < 3) {
                    builder.insert(0, "00");
                }

                builder.insert(builder.length() - 2, ".");

                while(builder.charAt(0) == 48 && builder.indexOf(".") != 1) {
                    builder.deleteCharAt(0);
                }

                return builder.toString();
            }
        }
    }

    public static Byte parseACK(byte[] msgPackage) {
        if(msgPackage[3] == 11) {
            byte ack = msgPackage[7];
            return Byte.valueOf(ack);
        } else {
            return null;
        }
    }

    public static Map<String, String> parseF55(String arqcOrArpc) {
        try {
            Map<String, String> map = new HashMap();
            String key = null;
            String value = null;
            int i = 0;

            while(i < arqcOrArpc.length()) {
                int startIndex = i;
                String firstByteHex = arqcOrArpc.substring(i, i + 2);
                int firstByte = Integer.valueOf(firstByteHex, 16).intValue();
                boolean is2Bytes = (firstByte & 31) == 31;
                if(is2Bytes) {
                    key = arqcOrArpc.substring(i, i + 4);
                    i += 4;
                } else {
                    key = arqcOrArpc.substring(i, i + 2);
                    i += 2;
                }

                int len = Integer.valueOf(arqcOrArpc.substring(i, i + 2), 16).intValue();
                if((len & 128) == 128) {
                    i += 2;
                    int lenOfLen = len & 127;
                    len = Integer.valueOf(arqcOrArpc.substring(i, i + lenOfLen * 2), 16).intValue();
                    i += lenOfLen * 2;
                } else {
                    i += 2;
                }

                i += len * 2;
                value = arqcOrArpc.substring(startIndex, i);
                if(!map.containsKey(key)) {
                    map.put(key, value);
                } else {
                    map.put(key, (String)map.get(key) + value);
                }
            }

            return map;
        } catch (Exception var11) {
            Log.e("parseF55", "参数错误, 参数内容: " + arqcOrArpc);
            var11.printStackTrace();
            return null;
        }
    }

    public static byte[] unpackingMessage(byte[] msgPackage) {
        if(msgPackage.length < 3) {
            return null;
        } else {
            int orderContentStartIndex = 7;
            byte[] totalLenArray = new byte[2];
            System.arraycopy(msgPackage, 1, totalLenArray, 0, 2);
            int totalLen = Integer.parseInt(Util.byte2HexStr(totalLenArray));
            int contentLen = totalLen - 4;
            if(msgPackage.length < orderContentStartIndex + contentLen) {
                return null;
            } else {
                byte[] content = new byte[contentLen];
                System.arraycopy(msgPackage, orderContentStartIndex, content, 0, contentLen);
                return content;
            }
        }
    }

    public static List<byte[]> parseLenDataList(byte[] data, int n) {
        List<byte[]> dataList = new ArrayList();

        int len;
        for(int index = 0; index < data.length; index += len) {
            byte[] lenArray = new byte[n];
            System.arraycopy(data, index, lenArray, 0, n);
            len = Integer.valueOf(Util.byte2HexStr(lenArray), 16).intValue();
            index += n;
            byte[] dataItem = new byte[len];
            System.arraycopy(data, index, dataItem, 0, len);
            dataList.add(dataItem);
        }

        return dataList;
    }

    public static List<byte[]> parseLenDataList(byte[] data, int[] lengthOfLenArray) {
        List<byte[]> dataList = new ArrayList();
        int indexOfData = 0;

        int len;
        for(int index = 0; indexOfData < data.length && index < lengthOfLenArray.length; indexOfData += len) {
            if(indexOfData == data.length) {
                return dataList;
            }

            int n = lengthOfLenArray[index++];
            byte[] dataItem;
            if(n > 0) {
                dataItem = new byte[n];
                System.arraycopy(data, indexOfData, dataItem, 0, n);
                len = Integer.valueOf(Util.byte2HexStr(dataItem), 16).intValue();
                indexOfData += n;
            } else {
                len = -n;
            }

            dataItem = new byte[len];
            System.arraycopy(data, indexOfData, dataItem, 0, len);
            dataList.add(dataItem);
        }

        return dataList;
    }

    public static List<String> byteArray2String(List<byte[]> list) {
        List<String> strList = new ArrayList(list.size());

        for(int i = 0; i < list.size(); ++i) {
            strList.add(new String((byte[])list.get(i)));
        }

        return strList;
    }

    public static String parseMap2String(Map<String, String> map, String keys, String separator) {
        StringBuilder builder = new StringBuilder("");

        for(int i = 0; i < keys.length(); ++i) {
            String key = keys.substring(i, i + 1);
            if(key.equals(" ")) {
                builder.append(separator);
            } else {
                String value = (String)map.get(key);
                if(value == null) {
                    builder.append(separator);
                } else {
                    builder.append(value);
                    builder.append(separator);
                }
            }
        }

        return builder.toString();
    }

    public static Map<String, String> parseMapData(String result) {
        HashMap map = new HashMap();

        try {
            int i = 0;

            while(i < result.length()) {
                String key = result.substring(i, i + 1);
                ++i;
                int len = Integer.parseInt(result.substring(i, i + 3));
                i += 3;
                String value = result.substring(i, i + len);
                i += len;
                map.put(key, value);
            }
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return map;
    }

    public static String[] parseMagneticCard(byte[] data) {
        String cardNo = "";
        String oneTrack = "";
        String twoTrack = "";
        String threeTrack = "";
        int cardNoArrayLen = data[0];
        if(cardNoArrayLen != 0) {
            byte[] cardNoArray = new byte[cardNoArrayLen];
            System.arraycopy(data, 1, cardNoArray, 0, cardNoArrayLen);
            cardNo = new String(cardNoArray, 0, cardNoArray.length);
        }

        int firstTrackArrayLen = data[1 + cardNoArrayLen];
        if(firstTrackArrayLen != 0) {
            byte[] firstTrackArray = new byte[firstTrackArrayLen];
            System.arraycopy(data, 1 + cardNoArrayLen + 1, firstTrackArray, 0, firstTrackArrayLen);
            oneTrack = new String(firstTrackArray, 0, firstTrackArray.length);
        }

        int twoTrackArrayLen = data[1 + cardNoArrayLen + 1 + firstTrackArrayLen];
        if(twoTrackArrayLen != 0) {
            byte[] twoTrackArray = new byte[twoTrackArrayLen];
            System.arraycopy(data, 1 + cardNoArrayLen + 1 + firstTrackArrayLen + 1, twoTrackArray, 0, twoTrackArray.length);
            twoTrack = new String(twoTrackArray, 0, twoTrackArray.length);
            if(!twoTrack.startsWith(cardNo)) {
                twoTrack = Util.bytesToHexString(twoTrackArray);
            }
        }

        int thirdTrackArrayLen = data[1 + cardNoArrayLen + 1 + firstTrackArrayLen + 1 + twoTrackArrayLen];
        if(thirdTrackArrayLen != 0) {
            byte[] thirdTrackArray = new byte[thirdTrackArrayLen];
            System.arraycopy(data, 1 + cardNoArrayLen + 1 + firstTrackArrayLen + 1 + twoTrackArrayLen + 1, thirdTrackArray, 0, thirdTrackArray.length);
            threeTrack = new String(thirdTrackArray, 0, thirdTrackArray.length);
            if(!twoTrack.startsWith(cardNo)) {
                threeTrack = Util.bytesToHexString(thirdTrackArray);
            }
        }

        return new String[]{cardNo, oneTrack, twoTrack, threeTrack};
    }

    public static String hashMacXOR(String value) {
        String result = "";
        List<String> list = new ArrayList();
        int count = value.length() / 32;
        int mod32 = value.length() % 32;
        int i;
        if(mod32 != 0) {
            StringBuilder builder = new StringBuilder(value);

            for(i = mod32; i < 32; ++i) {
                builder.append('0');
            }

            value = builder.toString();
            ++count;
        }

        int asciiValue;
        String str;
        if(count > 1) {
            for(i = 0; i < count; ++i) {
                String s = value.substring(32 * i, 32 * (i + 1));
                list.add(s);
            }

            BigInteger big1 = new BigInteger((String)list.get(0), 16);
            BigInteger big2 = new BigInteger((String)list.get(1), 16);
            str = big1.xor(big2).toString(16);

            for(asciiValue = 2; asciiValue < list.size(); ++asciiValue) {
                BigInteger b1 = new BigInteger(str, 16);
                BigInteger b2 = new BigInteger((String)list.get(asciiValue), 16);
                str = b1.xor(b2).toString(16);
            }
        } else {
            str = value;
        }

        mod32 = str.length() % 32;
        int r;
        if(mod32 != 0) {
            StringBuilder builder = new StringBuilder(str);

            for(r = mod32; r < 32; ++r) {
                builder.insert(0, '0');
            }

            str = builder.toString();
        }

        byte[] by = str.toUpperCase(Locale.US).getBytes();

        for(r = 0; r < by.length; ++r) {
            asciiValue = by[r];
            if(asciiValue < 0) {
                asciiValue += 256;
            }

            result = result + Integer.toHexString(asciiValue);
        }

        return result;
    }

    public static class Vi218DataPackageParseUtil {
        public Vi218DataPackageParseUtil() {
        }

        public static boolean checkPackageLenIsReachedMinRequirements(byte[] data) {
            return data.length >= 9;
        }

        public static boolean checkPackageHead(int i, byte[] data) {
            return data[i + 0] == 2 && data[i + 3] == 10 && data[i + 4] == 48;
        }

        public static int indexOfPackageHead(byte[] data) {
            for(int i = 0; i < data.length - 5; ++i) {
                boolean result = checkPackageHead(i, data);
                if(result) {
                    return i;
                }
            }

            return -1;
        }

        public static byte[] subByteForWrongPackageData(byte[] data) {
            return ByteUtil.arrayCopy(data, data.length - 5, 5);
        }

        public static int getPackageLength(byte[] data) {
            byte[] subByte = ByteUtil.arrayCopy(data, 1, 2);

            try {
                return Integer.parseInt(Util.byte2HexStr(subByte)) + 5;
            } catch (NumberFormatException var3) {
                return -1;
            }
        }

        public static byte calcLRC(byte[] data, int offset, int len) {
            if(data != null && len != 0) {
                byte lrc = 0;
                len += offset;

                for(int i = offset; i < len; ++i) {
                    lrc ^= data[i];
                }

                return lrc;
            } else {
                return 0;
            }
        }

        public static byte calcLRC(byte[] data, int dataLength) {
            return calcLRC(data, 0, dataLength);
        }
    }
}
